<?php
class controller_case extends controller_base {

	/**
	 * 列表
	 */
	public function action_index() {
        if (isset($_REQUEST['name'])) {
            $where = "name='".trim($_REQUEST['name'])."'";
        } else {
            $where = 1;
        }

		$model_case = new model_case();

        $data = $model_case->find_all($where, 'time DESC');

        $dailyData = array();
        foreach ($data as $item) {
            $dailyData[strtotime(date('Y-m-d', $item['time']))][] = $item;
        }

		$this->assign('dailyData', $dailyData);
		$this->assign('total', count($data));
		$this->assign('title', '事例列表');
	}
	
	/**
	 * 表单
	 */
	public function action_add() {
        //修改表单
        $model_case = new model_case();
        $res = $model_case->find_all('1 GROUP BY name', '', '', 'COUNT(*) total, name');
		
		//assign
		$this->assign('title', '添加事例');
		$this->assign('res', $res);
	}
	
	/**
	 * 添加
	 */
	public function action_add_do() {
		$model_case = new model_case();

        //name, time, note, ip, agent, createtime, isautotime
        $data = array(
            'createtime' => time(),
            'ip' => $_SERVER['REMOTE_ADDR'],
            'agnet' => $_SERVER['HTTP_USER_AGENT'],
        );

        $data['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
        $data['time'] = isset($_REQUEST['time']) ? strtotime($_REQUEST['time']) : time();
        $data['note'] = isset($_REQUEST['note']) ? trim($_REQUEST['note']) : '';

        $data['isautotime'] = $data['time'] === $data['createtime'] ? 1 : 0;

		$model_case->create($data);
		
		//log
		new model_logs(print_r($data, 1));
			
		$this->message('添加成功', '?m=case');
	}

	/**
	 * 表单
	 */
	public function action_edit() {
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        if ($id === 0) {
            $this->message('参数错误', '?m=case');
        }

        $model_case = new model_case();
        $data = $model_case->find("id=$id");
		
		//assign
		$this->assign('title', '修改事例');
		$this->assign('data', $data);
	}

    public function action_edit_do() {
        $oldtime = isset($_REQUEST['oldtime']) ? intval($_REQUEST['oldtime']) : 0;
        $time = isset($_REQUEST['time']) ? strtotime($_REQUEST['time']) : 0;

        //name, time, note, ip, agent, createtime, isautotime
        $data = array();
        $data['id'] = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
        $data['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
        $data['note'] = isset($_REQUEST['note']) ? trim($_REQUEST['note']) : '';

        if ($oldtime === 0 || $time === 0 || $data['id'] === 0) {
            $this->message('参数错误', '?m=case');
        }

        if ($time !== $oldtime)
        {
            $data['time'] = $time;
            $data['isautotime'] = 0;
        }

		$model_case = new model_case();
		$model_case->update($data);
		
		//log
		new model_logs(print_r($data, 1));
			
		$this->message('添加成功', '?m=case');
    }

    public function action_delete() {
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

        if ($id === 0) {
            $this->message('参数错误', '?m=case');
        }

        $model_case = new model_case();
        
        $data = $model_case->find("id=$id");

		$model_case->delete("id=$id");

		//log
		new model_logs(print_r($data, 1));
			
		$this->message('删除成功', '?m=case');
    }

}
